package DAO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import sun.jdbc.odbc.ee.DataSource;

public class JNDI_DAO {
	private static Connection conn=null;
	private static Context ctx=null;
	{
		getConnect();
	}
	public JNDI_DAO() {
		
	}
	public static Context getContext()
	{
		if(ctx!=null)
			return ctx;
		try {
			ctx = new InitialContext();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ctx;
	}
	public static Connection getConnect()
	{
        if(conn!=null)
        	return conn;
		try {
			getContext();
	        Object dataSourceRef=ctx.lookup("java:jboss/datasources/MySqlDS");
	        DataSource ds=(DataSource)dataSourceRef;
	        conn=ds.getConnection();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return conn;
	}
}
